package threading;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xwl
 * Date: 2023-08-08
 * Time: 21:51
 */
public class ThreadDemo11 {
    // volatile修饰的变量会禁止编译器的优化(例:可以禁止指令重排序)
    volatile public static int flag = 0;

    public static void main(String[] args) {
        Thread t1 = new Thread( () -> {
            // 由于load的开销很大,每次load的结果都一样
            // 于是编译器就把load给优化掉了(只有第一次执行load才真正执行了)
            while (flag == 0) {
                // 空着
            }
            System.out.println("循环结束!t1线程程结束!");
        });

        Thread t2 = new Thread( () -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个整数:");
            flag = scanner.nextInt();
        });

        t1.start();
        t2.start();
    }

}
